package com.zzz.offer.longestPalindrome;


import java.util.*;

public class Solution {

    public static void main(String[] args) {
        System.out.println(new Solution().longestPalindrome("accb"));
    }

    public String longestPalindrome(String s) {
        if (s == null || s.equals("")) {
            return s;
        }

        int maxLen = 0;

        String longPalindrome = "";

        for (int i = 0; i < s.length(); i++) {
            for (int j = i + 1; j <= s.length(); j++) {
                String sub = s.substring(i, j);
                if (sub.length() > maxLen && isPalindrome(sub)) {
                    maxLen = sub.length();
                    longPalindrome = sub;
                }
            }
        }

        return longPalindrome;
    }


    public boolean isPalindrome(String s) {
        if (s.length() == 1) {
            return true;
        }
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
                return false;
            }
        }
        return true;
    }
}
